package com.jiuyin.dianjing.db.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.BaseColumns;
import android.text.TextUtils;

import com.jiuyin.dianjing.db.helper.DataHelper;
import com.jiuyin.dianjing.db.table.SearchHistoryColumn;

import java.util.List;

import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;

import static com.jiuyin.dianjing.constant.HelperConstant.CONTENT_TYPE;
import static com.jiuyin.dianjing.constant.HelperConstant.CONTENT_TYPE_ITEM;
import static com.jiuyin.dianjing.constant.HelperConstant.PROVIDER_AUTHORITY;
import static com.jiuyin.dianjing.constant.HelperConstant.TYPE_ORDER;
import static com.jiuyin.dianjing.constant.HelperConstant.TYPE_ORDER_ITEM;


/**
 * @author Administrator
 */
public class AppContentProvider extends ContentProvider {
    private static final UriMatcher S_URI_MATCHER;

    static {
        S_URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
        S_URI_MATCHER.addURI(PROVIDER_AUTHORITY, SearchHistoryColumn.TABLE_NAME, TYPE_ORDER);
        S_URI_MATCHER.addURI(PROVIDER_AUTHORITY, SearchHistoryColumn.TABLE_NAME + "/#", TYPE_ORDER_ITEM);
    }

    private Context mContext;
    private SQLiteDatabase mDatabase;

    @Override
    public boolean onCreate() {
        mContext = getContext();
        DataHelper aliDataHelper = new DataHelper(mContext);
        mDatabase = aliDataHelper.getWritableDatabase();
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        List<String> path = uri.getPathSegments();
        String table = path.get(0);
        int type = S_URI_MATCHER.match(uri);
        switch (type) {
            case TYPE_ORDER_ITEM:
                String id = path.get(1);
                selection = BaseColumns._ID + "=" + id
                        + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
                break;
            case TYPE_ORDER:
                break;
            default:
                throw new IllegalArgumentException(uri.toString());
        }
        return mDatabase.query(table, projection, selection, selectionArgs, null, null, sortOrder, null);

    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        int type = S_URI_MATCHER.match(uri);
        switch (type) {
            case TYPE_ORDER:
                return CONTENT_TYPE;
            case TYPE_ORDER_ITEM:
                return CONTENT_TYPE_ITEM;
            default:
                throw new IllegalArgumentException(uri.toString());
        }
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        String table = uri.getPathSegments().get(0);
        long id = mDatabase.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_REPLACE);
        if (id > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
            return uri;
        }
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String where, @Nullable String[] whereArgs) {
        List<String> path = uri.getPathSegments();
        String table = path.get(0);
        int count;
        int type = S_URI_MATCHER.match(uri);
        switch (type) {
            case TYPE_ORDER_ITEM:
                String id = path.get(1);
                count = mDatabase.delete(table, BaseColumns._ID + "=" + id
                        + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
                break;
            case TYPE_ORDER:
                count = mDatabase.delete(table, where, whereArgs);
                break;
            default:
                throw new IllegalArgumentException(uri.toString());
        }
        if (count > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return count;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String
            where, @Nullable String[] whereArgs) {
        List<String> path = uri.getPathSegments();
        String table = path.get(0);
        int count;
        int type = S_URI_MATCHER.match(uri);
        switch (type) {
            case TYPE_ORDER_ITEM:
                String id = path.get(1);
                count = mDatabase.update(table, values, BaseColumns._ID + "=" + id
                        + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
                break;
            case TYPE_ORDER:
                count = mDatabase.update(table, values, where, whereArgs);
                break;
            default:
                throw new IllegalArgumentException(uri.toString());
        }
        if (count > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return count;
    }
}
